package com.kzd.sdk.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.kzd.sdk.callback.KZD_OnItemChildClickListener;
import com.kzd.sdk.callback.KZD_OnItemClickListener;
import com.kzd.sdk.entity.KZD_GameService;
import com.kzd.sdk.ui.adapter.holder.GameServiceHolder;
import com.kzd.sdk.utils.AppUtils;

import java.util.ArrayList;
import java.util.List;


public class KZD_GameServiceAdapter extends RecyclerView.Adapter<GameServiceHolder> {

    private List<KZD_GameService.Service> data = new ArrayList<>();

    private final Context mContext;
    private final int mType;

    private KZD_OnItemClickListener mOnItemClickListener;
    private KZD_OnItemChildClickListener mOnItemChildClickListener;

    public KZD_GameServiceAdapter(Context context, int type) {
        this.mContext = context;
        this.mType = type;
    }

    @NonNull
    @Override
    public GameServiceHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(AppUtils.getLayoutId(mContext, "kzd_item_game_service"), parent, false);
        return new GameServiceHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull GameServiceHolder holder, int position) {
        KZD_GameService.Service itemData = get(position);
        holder.tvGameServiceName.setText(itemData.getServer_name());
        holder.tvGameServiceTime.setText(itemData.getTime1());
        if (mType == 1) {
            holder.tvGameServiceRemind.setVisibility(View.VISIBLE);
        } else {
            holder.tvGameServiceRemind.setVisibility(View.INVISIBLE);
        }
        if (itemData.getRemind_rs() == 0) {
            holder.tvGameServiceRemind.setText("提醒");
        } else {
            holder.tvGameServiceRemind.setText("取消提醒");
        }
        holder.tvGameServiceRemind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemChildClickListener != null) {
                    mOnItemChildClickListener.onClick(v, position);
                }
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onClick(v, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void setNewInstance(List<KZD_GameService.Service> gameServiceList) {
        if (gameServiceList == null) {
            this.data = new ArrayList<>();
        } else {
            this.data = gameServiceList;
        }
        notifyDataSetChanged();
    }

    public KZD_GameService.Service get(int position) {
        return data.get(position);
    }

    public void setOnItemClickListener(KZD_OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public void setOnItemChildClickListener(KZD_OnItemChildClickListener onItemChildClickListener) {
        this.mOnItemChildClickListener = onItemChildClickListener;
    }
}
